package com.pw.gmall.realtime.app;

import com.pw.gmall.realtime.utils.KafkaUtils;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

/**
 * @Author: linux_future
 * @since: 2022/3/15
 **/
public abstract class BaseApp {

    public void init(int portUI, int parallelNumber, String checkPointDir, String groupName, String topic){
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        Configuration config = new Configuration();
        config.setInteger("rest.port", portUI);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(config);
        env.setParallelism(parallelNumber);


        //开启checkpoint
        env.enableCheckpointing(3000);
        //设置状态后端
        env.setStateBackend(new HashMapStateBackend());
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/flink/ck/gmall/"+checkPointDir);

        env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500);
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setCheckpointTimeout(30*1000);
        env.getCheckpointConfig().setExternalizedCheckpointCleanup(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);

        FlinkKafkaConsumer<String> data = KafkaUtils.getKafkaSource(groupName, topic);
        DataStreamSource<String> stream = env.addSource(data);

        handler(env,stream);

        try {
            env.execute(checkPointDir);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    protected abstract void handler(StreamExecutionEnvironment env, DataStreamSource<String> stream);


}
